package com.foo.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.example.www.FacadeInterfaceServiceStub.GetMovieInfoByGenreFault;
import org.example.www.FacadeInterfaceServiceStub.GetMovieInfoByNameFault;
import org.example.www.FacadeInterfaceServiceStub.GetMovieInfoByTheatreFault;
import org.example.www.FacadeInterfaceServiceStub.GetMovieInfoByTimeFault;
import org.example.www.FacadeInterfaceServiceStub.GetMovieInfoByTypeFault;
import org.example.www.FacadeInterfaceServiceStub.MoviesFacade;
import org.example.www.FacadeInterfaceServiceStub.MoviesFacadeList;
import org.example.www.FacadeWSClient;
import org.example.www.GetMovieInfoByGenreFaultException;
import org.example.www.GetMovieInfoByNameFaultException;
import org.example.www.GetMovieInfoByTheatreFaultException;
import org.example.www.GetMovieInfoByTimeFaultException;
import org.example.www.GetMovieInfoByTypeFaultException;


/**
 * Servlet implementation class MovieFacadeServlet
 */
public class MovieFacadeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MovieFacadeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String sortBased = request.getParameter("SortBased");
		String paramName = "";
		MoviesFacadeList movies = null;
		FacadeWSClient client = new FacadeWSClient();
		RequestDispatcher dispatcher;
		
		if(sortBased.equalsIgnoreCase("movieName"))
		{
			paramName = request.getParameter("movieName");
			MoviesFacade res = null;
			try {
				res = client.getMovieInfoByName(paramName);
			} catch (GetMovieInfoByNameFaultException e) {
				GetMovieInfoByNameFault fault = new GetMovieInfoByNameFault();
				String exceptionString = fault.getGetMovieInfoByNameFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
				dispatcher.forward(request, response);
			}
			movies = new MoviesFacadeList();
			movies.addMovieList(res);
		}
		else if(sortBased.equalsIgnoreCase("genre"))
		{
			paramName = request.getParameter("genre");
			
			try {
				movies = client.getMovieInfoByGenre(paramName);
			} 
			catch (GetMovieInfoByGenreFaultException e) {
				GetMovieInfoByGenreFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetMovieInfoByGenreFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
				dispatcher.forward(request, response);
			}
		}
		else if(sortBased.equalsIgnoreCase("theatreName"))
		{
			paramName = request.getParameter("theatreName");
						
			try 
			{
				movies = client.getMovieInfoByTheatre(paramName);
			} 
			catch (GetMovieInfoByTheatreFaultException e) {
				GetMovieInfoByTheatreFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetMovieInfoByTheatreFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
				dispatcher.forward(request, response);
			}
		}
		else if(sortBased.equalsIgnoreCase("showTime"))
		{
			paramName = request.getParameter("showTime");
						
			try {
				movies = client.getMovieInfoByTime(paramName);
			} catch (GetMovieInfoByTimeFaultException e) {
				GetMovieInfoByTimeFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetMovieInfoByTimeFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
				dispatcher.forward(request, response);
			}
		}
		else if(sortBased.equalsIgnoreCase("movie_Rating"))
		{
			paramName = request.getParameter("movie_Rating");
						
			try {
				movies = client.getMovieInfoByRating(paramName);
			} catch (GetMovieInfoByTypeFaultException e) {
				GetMovieInfoByTypeFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetMovieInfoByTypeFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
				dispatcher.forward(request, response);
			}
		}
		
	    session.setAttribute("MovieData", movies.getMovieList());
	      
	    dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
		dispatcher.forward(request, response);
	}

}
